package com.digital.welcome.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.digital.welcome.entity.Application;
import com.digital.welcome.entity.User;
import com.digital.welcome.mapper.ApplicationMapper;
import com.digital.welcome.mapper.UserMapper;
import com.digital.welcome.response.Result;
import com.digital.welcome.service.ApplicationService;
import com.digital.welcome.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalTime;
import java.util.HashMap;
import java.util.List;


@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, Application> implements ApplicationService {

    @Autowired
    private ApplicationMapper applicationMapper;

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserService userService;

    @Autowired
    private ApplicationService applicationService;

    @Override
    public Application getApplicationByUserName(String userName) {
        return applicationMapper.getApplicationByUserName(userName);
    }

    @Override
    public boolean insertApplication(Application application) {
        return applicationMapper.insertApplication(application);
    }

    @Override
    public String getAll() {
        Integer count = null;
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.isNotNull("category");
        count = userMapper.selectCount(queryWrapper);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(count));
    }

    @Override
    public String getNumber() {
        Integer count = null;
        QueryWrapper<Application> queryWrapper = new QueryWrapper<>();
        count = applicationMapper.selectCount(queryWrapper);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(count));
    }

    @Override
    public Boolean getState(String userName) {
        QueryWrapper<Application> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        Integer count = applicationMapper.selectCount(queryWrapper);
        return count != 0;
    }

    @Override
    public String getStudents() {
        List<User> userList = userMapper.getStudents();
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(userList));
    }

    @Override
    public String getNStudents() {
        List<User> userList = userMapper.getNStudents();
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(userList));
    }

    @Override
    public String getCollegeNStudents(String college) {
        if (college == null) {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("查询失败"));
        }
        List<User> userList = userMapper.getCollegeNStudents(college);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(userList));
    }

    @Override
    public String getCollegeStudents(String college) {
        if (college == null) {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("查询失败"));
        }
        List<User> userList = userMapper.getCollegeStudents(college);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(userList));
    }

    @Override
    public String selectConfirm(String userName) {
        User user = userMapper.selectApplicationByUser(userName);
        if (user == null) {
            User userByUsername = userMapper.getUserByUsername(userName);
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("未报道").setData(userByUsername));
        }
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(user));
    }

    @Override
    public String getApplication(String userName) {
        User user = userService.getUserByUsername(userName);
        if (user == null) {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("用户不存在"));
        }
        Application newApplication = applicationService.getApplicationByUserName(userName);
        if (newApplication == null) {
            return JSON.toJSONString(new Result<>().setCode(401).setMessage("用户还未报道"));
        }
        HashMap<String, Object> map = new HashMap<>();
        map.put("user", user);
        map.put("application", newApplication);
        return JSON.toJSONString(new Result<>().setCode(200).setMessage("查询成功").setData(map));
    }

    //    MyProducer producer = new MyProducer();
    @Override
    public String doApplication(Application application) {
        Application userApplication = applicationService.getApplicationByUserName(application.getUserName());
        if (userApplication != null) {
            return JSON.toJSONString(new Result<>().setCode(400).setMessage("已报名"));
        } else {
            LocalTime localTime = LocalTime.now();
            application.setTime(localTime);
            boolean res = applicationService.insertApplication(application);
            if (res) {
                //报名的同时更新前端数据
                //生产者生产消息
//                KafkaMessage kafkaMessage = new KafkaMessage();
//                kafkaMessage.setUserName(application.getUserName());
//                kafkaMessage.setName(userService.getUserByUsername(application.getUserName()).getName());
//                kafkaMessage.setFlag(0);
//                producer.send(kafkaMessage);
//                OneToManyWebSocket webSocket = new OneToManyWebSocket();
//                webSocket.sendMessage(kafkaMessage,clients);
                return JSON.toJSONString(new Result<>().setCode(1).setMessage("报名成功").setData(application));
            } else {
                return JSON.toJSONString(new Result<>().setCode(401).setMessage("报名失败"));
            }
        }
    }
}
